package com.gitee.huanminabc.utils_tools.security.vo;


import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.gitee.huanminabc.utils_tools.security.entity.UserEntity;
import com.google.gson.annotations.Expose;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * 重写UserDetails返回的用户信息
 * SpringSecurity返回的用户信息实体类
 */
@Data
@NoArgsConstructor
@Slf4j
@ApiModel(value = "LoginUserVo",description = "用户信息和权限信息")
public class LoginUserVo implements UserDetails,Cloneable, Serializable {
    @ApiModelProperty(value = "用户信息")
    private UserEntity sysUser;//用户信息
    @ApiModelProperty(value = "权限角色code")
    private List<String> permissions;//权限信息

    //不序列化
    @JSONField(serialize = false) // 标记该字段不参与序列化
    @JsonIgnore // 标记该字段不参与序列化
    @Expose(serialize = false) // 标记该字段不参与序列化
    private transient  List<SimpleGrantedAuthority> authorities;



    public LoginUserVo(UserEntity sysUser, List<String> permissions) {
        this.sysUser = sysUser;
        this.permissions = permissions;
    }

    /**
     * 获取权限信息
     */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        // 权限为空的时候才往遍历，不为空直接返回
        if (authorities != null) {
            return authorities;
        }
        if (permissions == null) {
            return null;
        }
        //把permissions中String类型的权限信息封装成SimpleGrantedAuthority对象
        authorities = new ArrayList<>();
        for (String permission : permissions) {
            SimpleGrantedAuthority authority = new SimpleGrantedAuthority(permission);
            authorities.add(authority);
        }
        return authorities;
    }

    /**
     * 获取密码
     */
    @Override
    public String getPassword() {
        return sysUser.getPassword();
    }

    /**
     * 获取用户名
     */
    @Override
    public String getUsername() {
        return sysUser.getAccount();
    }

    /**
     * 判断是否过期
     */
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    /**
     * 是否锁定
     */
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    /**
     * 是否没有超时
     */
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    /**
     * 是否可用
     */
    @Override
    public boolean isEnabled() {
        return true;
    }


    //深度拷贝
    @Override
    public Object clone() {
        Object object = null;
        try {
            // 序列化
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(bos);
            oos.writeObject(this);
            // 反序列化
            ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
            ObjectInputStream ois = new ObjectInputStream(bis);
            object = ois.readObject();
        } catch (IOException | ClassNotFoundException e) {
           log.error("",e);
        }
        return object;
    }
}
